package com.xiaopeng.system.test.api;

import com.xiaopeng.common.excel.ExcelDynamicColumn;
import com.xiaopeng.system.common.annotation.ResponseWrapper;
import com.xiaopeng.system.test.model.TestReqDTO;
import com.xiaopeng.common.excel.ExcelExportUtil;
import com.xiaopeng.system.test.model.TestVO;
import com.xiaopeng.system.test.service.DemoService;
import com.xiaopeng.system.test.service.TestService;
import com.xiaopeng.system.test.model.TestExcelVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "excel")
@RestController
@RequestMapping("/excel")
@ResponseWrapper
public class ExcelController {

    @Resource
    TestService testService;
    @Resource
    DemoService demoService;

    @Operation(summary = "List导出固定列")
    @PostMapping("/test1")
    public void test1(@RequestBody TestReqDTO reqDTO, HttpServletResponse response) throws Exception {
        List<TestVO> testExcelVOList = testService.selectList(reqDTO);
        ExcelExportUtil.httpResponse("文件名_固定列导出")
            .addSheet("sheet1_列表导出", testExcelVOList, TestVO.class)
            .addSheet("sheet2_列表导出(数据列自动转换为导出类)", testExcelVOList, TestExcelVO.class)
            .addSheet("sheet3_分页导出",  reqDTO, testService::selectPage, TestVO.class)
            .addSheet("sheet4_分页导出(数据列自动转换为导出类)",  reqDTO, testService::selectPage, TestExcelVO.class)
            .execute();
            }

    @Operation(summary = "Page导出固定列")
    @PostMapping("/test2")
    public void test2(@RequestBody TestReqDTO reqDTO, HttpServletResponse response){

        ExcelExportUtil.httpResponse("文件名_固定列导出")
            .addSheet("sheet名字1",  reqDTO, testService::selectPage, TestExcelVO.class)
            .execute();

//        ExcelExportUtil.file("D:\\导出测试2.xlsx")
//                .pageSize(1)
//                .execute(reqDTO, testService::selectPage, TestExcelVO.class);
    }
    @Operation(summary = "Page导出固定列")
    @PostMapping("/test3")
    public void test3(@RequestBody TestReqDTO reqDTO, HttpServletResponse response){
        List<String> warehouseNameList = new ArrayList<>();

        ExcelExportUtil.httpResponse("文件名_动态列导出")
            .addSheet("sheet名字1", reqDTO, testService::selectPage, new ExcelDynamicColumn<TestVO>() {
                @Override
                public List<List<String>> head() {
                    List<String> list = new ArrayList<>();
                    list.add("列名一");
                    list.add("列名二");
                    // ...
                    // 动态列 各个仓库名称
                    for (String warehouseName : warehouseNameList) {
                        list.add(warehouseName);
                    }
                    return List.of(list);
                }

                @Override
                public List<Object> columnConvert(TestVO resDTO) {
                    List<Object> list = new ArrayList<>();
                    list.add(resDTO.getType());
                    list.add(resDTO.getStatus());
                    // ...
                    // 动态列 各个库存数量
                    for (Integer stockNum : resDTO.getStockNumList()) {
                        list.add(stockNum);
                    }
                    return list;
                }
            })
            .execute();
    }

    @Operation(summary = "多sheet页 等特殊的导出")
    @PostMapping("/test4")
    public void test4(@RequestBody TestReqDTO reqDTO, HttpServletResponse response){
//        ExcelExportUtil.handleResponse("多sheet页导出", response, excelWriter -> {
//            // 收集分页数据转为List
//            List<TestExcelVO> testlExcelList = ExcelExportUtil.collectPage(reqDTO, testService::selectPage);
//            // 可选非必写 如果有需要 映射枚举值字段为对应文字含义
//            ExcelExportUtil.excelViewConvert(TestExcelVO.class, testlExcelList);
//
//            // 第二sheet数据
//            List<DemoExcelVO> demoExcelVOList = ExcelExportUtil.collectPage(reqDTO, demoService::selectPage);
//            // 可选非必写 如果有需要 映射枚举值字段为对应文字含义
//            ExcelExportUtil.excelViewConvert(DemoExcelVO.class, demoExcelVOList);
//
//            WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "第一页").head(TestExcelVO.class).httpResponse();
//            excelWriter.write(testlExcelList, writeSheet1);
//            WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "第二页").head(TestExcelVO.class).httpResponse();
//            excelWriter.write(demoExcelVOList, writeSheet2);
//        });
    }




}
